import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/user.vue')
  },
  {
    path: '/home',
    name: 'home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/home.vue')
  },
  {
      path:'/add',
      component:() => import ('../views/add.vue')
  },
  {
      path:'/detail',
      component:() => import('../views/detail.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) => {
    let writePath = ['/user','/home'];
    if(writePath.includes(to.path)){
        next();
    }else{
        let username = localStorage.getItem('username');
        if(username){
            next();
        }else{
            next('/user')
        }
    }
})

export default router
